<?php

include_once 'log_text.php';

const DB_HOST = 'localhost';
const DB_PORT = '3306';
const DB_USER = 'cdapp';
const DB_PASSWD = 'gzzxjjwt1';
const DB_NAME = 'cdapp';

function exec_sql($sql) {
  log_text('exec_sql');
  if (defined('IS_DEBUG') && IS_DEBUG) {
    log_text($sql);
  }

  $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
  if ($con->connect_error) {
    return "连接失败: " . $con->connect_error;
  }
  $con->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
  $result = $con->query($sql);
  if ($result) {
    // 输出数据
    if ($result->num_rows > 0) {
      $output_data = array();
      while ($row = $result->fetch_assoc()) {
        array_push($output_data, $row);
      }
      return $output_data;
    } else if (is_bool($result)) {
      return $result;
    }
    return false;
  } else {
    return false;
  }
}

function exec_sql_array($sql_arr) {
  $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
  if ($con->connect_error) {
    // die("连接失败: " . $con->connect_error);
    return "连接失败: " . $con->connect_error;
  }
  $con->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
  $results = array();
  foreach ($sql_arr as $sql) {
    $result = $con->query($sql . ';');
    array_push($results, $result);
  }
  return $results;
}
